import cv2
import numpy as np
import pyvirtualcam

# 设置虚拟摄像头的分辨率和帧率
width, height, fps = 1280, 720, 30

# 创建一个空的黑色画布
frame = np.zeros((height, width, 3), dtype=np.uint8)

# 加载图片
image = cv2.imread(r"C:\Users\happy\Desktop\5.jpeg")  # 替换为你的图片路径
if image is None:
    raise Exception("无法加载图片，请检查路径！")
image = cv2.resize(image, (400, 300))  # 调整图片大小

# 图片的初始位置
image_x, image_y = 100, 100

# 文字的初始位置和移动速度
text = "Hello, pyvirtualcam!"
text_x, text_y = 50, 600
text_speed = 5

# 初始化虚拟摄像头
with pyvirtualcam.Camera(width=width, height=height, fps=fps, fmt=pyvirtualcam.PixelFormat.BGR) as cam:
    print(f"Using virtual camera: {cam.device}")

    while True:
        # 清空画布（黑色背景）
        frame[:] = (0, 0, 0)

        # 将图片绘制到画布上
        frame[image_y:image_y + image.shape[0], image_x:image_x + image.shape[1]] = image

        # 绘制文字
        font = cv2.FONT_HERSHEY_SIMPLEX
        font_scale = 2
        font_color = (0, 255, 0)  # 绿色文字
        thickness = 3
        text_size = cv2.getTextSize(text, font, font_scale, thickness)[0]
        text_width, text_height = text_size

        # 更新文字位置（让文字移动）
        text_x += text_speed
        if text_x > width or text_x < -text_width:  # 如果文字移出屏幕，则从另一侧重新进入
            text_x = -text_width

        # 在画布上绘制文字
        cv2.putText(frame, text, (text_x, text_y), font, font_scale, font_color, thickness)

        # 将画布发送到虚拟摄像头
        cam.send(frame)

        # 等待下一帧
        cam.sleep_until_next_frame()
